//
//  LaunchViewController.m
//  HomeIn
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 ZZP. All rights reserved.
//

#import "LaunchViewController.h"
#import "AppDelegate.h"

@interface LaunchViewController ()<UIScrollViewDelegate,UIApplicationDelegate>

@property (nonatomic, retain) UIScrollView *myScrollView;
@property (nonatomic, retain) UIPageControl *pageC;
@property (nonatomic, retain) UIImageView *logoView;

@end

@implementation LaunchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self gotoLaunch];
}

- (void)gotoLaunch{
    
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentPath = [array lastObject];
    NSString *eyeWinkTxt = [documentPath stringByAppendingPathComponent:@"yindaoye.txt"];
    NSString *content = @"存在此文件";
    
    BOOL result = [content writeToFile:eyeWinkTxt atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if(result){
        NSLog(@"写入成功");
    }
    else{
        NSLog(@"写入失败");
    }
    
    [self makeLaunchView];
    
}



- (void)scroll:(NSTimer *)timer{
    
    [UIView animateWithDuration:2 animations:^{
        
        self.myScrollView.alpha=1;
        
    }completion:^(BOOL finished){
        
        [timer invalidate];
    }];
}


#pragma mark- 滚动图片和page显示
-(void)makeLaunchView{
    
    self.myScrollView = [[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myScrollView.contentSize = CGSizeMake(self.view.frame.size.width * 3, self.view.frame.size.height);
    self.myScrollView.pagingEnabled = YES;
    self.myScrollView.showsHorizontalScrollIndicator = NO;
    self.myScrollView.showsVerticalScrollIndicator = NO;
    self.myScrollView.delegate = self;
    
    //动图
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"kaishi1.tiff"],
                         [UIImage imageNamed:@"kaishi2.tiff"],
                         [UIImage imageNamed:@"kaishi3.tiff"],
                         [UIImage imageNamed:@"kaishi4.tiff"],
                         [UIImage imageNamed:@"kaishi5.tiff"],
                         [UIImage imageNamed:@"kaishi6.tiff"],
                         [UIImage imageNamed:@"kaishi7.tiff"],nil];
    NSArray *gifArray1 = [NSArray arrayWithObjects:[UIImage imageNamed:@"1.tiff"],
                          [UIImage imageNamed:@"2.tiff"],
                          [UIImage imageNamed:@"3.tiff"],
                          [UIImage imageNamed:@"4.tiff"],
                          [UIImage imageNamed:@"5.tiff"],
                          [UIImage imageNamed:@"6.tiff"],
                          [UIImage imageNamed:@"7.tiff"],
                          [UIImage imageNamed:@"8.tiff"],
                          [UIImage imageNamed:@"9.tiff"],
                          [UIImage imageNamed:@"10.tiff"],
                          [UIImage imageNamed:@"11.tiff"],
                          [UIImage imageNamed:@"12.tiff"],
                          [UIImage imageNamed:@"13.tiff"],
                          [UIImage imageNamed:@"14.tiff"],
                          [UIImage imageNamed:@"15.tiff"],
                          [UIImage imageNamed:@"16.tiff"],
                          [UIImage imageNamed:@"17.tiff"],
                          [UIImage imageNamed:@"18.tiff"],
                          [UIImage imageNamed:@"19.tiff"],
                          [UIImage imageNamed:@"20.tiff"],
                          [UIImage imageNamed:@"21.tiff"],
                          [UIImage imageNamed:@"22.tiff"],nil];
    
    [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(scroll:) userInfo:nil repeats:YES];
    
    [self.view addSubview:self.myScrollView];
    
    for(int i = 0; i < 3 ; i++){
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(i * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height)];
        
        if (i == 0) {
            imageview.image = [UIImage imageNamed:@"ydy.png"];
        }
        
        if (i == 1) {
            imageview.animationImages = gifArray1; //动画图片数组
            imageview.animationDuration = 2; //执行一次完整动画所需的时长
            imageview.animationRepeatCount = 0;  //动画重复次数
            [imageview startAnimating];
            imageview.userInteractionEnabled = YES;
        }
        if (i == 2) {
            imageview.animationImages = gifArray; //动画图片数组
            imageview.animationDuration = 0.7; //执行一次完整动画所需的时长
            imageview.animationRepeatCount = 0;  //动画重复次数
            [imageview startAnimating];
            imageview.userInteractionEnabled = YES;
            
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(self.view.frame.size.width / 3, 3 * self.view.frame.size.height/4 - 40 * HEIGHTSCALE, self.view.frame.size.width / 3, HEIGHTSCALE * 30);
            [imageview addSubview:button];
            button.backgroundColor = [UIColor colorWithRed:0.149 green:0.843 blue:0.643 alpha:0.630];
            button.layer.masksToBounds = YES;
            button.layer.cornerRadius = 20;
            button.layer.borderWidth = 1;
            button.layer.borderColor = [UIColor colorWithRed:0.000 green:1.000 blue:0.000 alpha:0.530].CGColor;
            
            [button setTitle:@"立即进入" forState:UIControlStateNormal];
            
            [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
        }
        
        [self.myScrollView addSubview:imageview];
        
    }
    
    self.pageC = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDTH / 2 -  100, 600 * HEIGHTSCALE,  200 * WIDTHSCALE,  40 * HEIGHTSCALE)];
    self.pageC.numberOfPages = 3;
    self.pageC.currentPage = 0;
    self.pageC.pageIndicatorTintColor = [UIColor grayColor];
    self.pageC.currentPageIndicatorTintColor = [UIColor orangeColor];
    [self.view addSubview:self.pageC];

}


-(void)buttonAction
{
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [app goMain];
    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    //  当图片全部滑完后 又向后多滑了100 的时候就做下一个动作
    self.pageC.currentPage = self.myScrollView.contentOffset.x / WIDTH;
   
}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
